import { XScrollableContainer } from './../../components/x-scrollable-container';
import { Order, Orderitem } from './../../models/order';
import { ExpeditionOrderDishManage } from './../../components/expeditionOrderDishManage';
import { AppStore } from './../../services/appStore';
import { ActivatedRoute, Params } from '@angular/router';
import { Component, OnInit, Input, ViewChild, ChangeDetectorRef } from '@angular/core';
import { Subject, BehaviorSubject, Observable, Subscription } from 'rxjs';

@Component({
    selector: 'suspended-orderitems',
    styleUrls: ['./index.scss'],
    template: `
        <div class="suspended_orderitems_container">
            <x-scrollable-container (change)="PagingMeaageChange($event)">
                <div class="item" item *ngFor="let item of orderItems;trackBy:trackById" (click)="item.deliverTime||addKitchenOrderToExpeditionOrderDish(item)">
                    <suspended-orderitem  [orderItem]="item" [order]="order" ></suspended-orderitem>
                </div>
                <div class="item tip" item *ngIf="order&&order.description">
                    备注：{{order&&order.description}}
                </div>
                <div class="center" *ngIf="orderItems&&orderItems.length==0">请选择订单</div>
            </x-scrollable-container>
            
            <div class="bottom" *ngIf="pagingInfo&&pagingInfo.previousCount || pagingInfo&&pagingInfo.nextCount">
                <page-btn [btnType]="'up-down'" [upNum]="pagingInfo&&pagingInfo.previousCount" [downNum]="pagingInfo&&pagingInfo.nextCount" (onUp)="up()" (onDown)="down()"></page-btn>
            </div>
        </div>        
    `
})
export class SuspendedOrderItems implements OnInit {
    @ViewChild(XScrollableContainer)
    private xScrollableContainer: XScrollableContainer;
    pagingInfo: {
        previousCount: number, nextCount: number, turningPage(count)
    }


    constructor(private changeDetectorRef: ChangeDetectorRef, private activeRouter: ActivatedRoute, private appStore: AppStore) { }
    @Input() orderItems: any;
    @Input() order: Order;
    PagingMeaageChange(event) {
        this.pagingInfo = event;
    }

    ngOnChanges(change) {
        if (change.orderItems) {
            this.xScrollableContainer.childrenChanged();
        }
    }

    ngOnInit() {

    }

    private trackById(index: number, orderItem: any) {
        return orderItem.id;
    }

    addKitchenOrderToExpeditionOrderDish(orderItem: Orderitem) {
        if (orderItem.deliverTime) {
        } else {
            ExpeditionOrderDishManage.current.addKitchenOrderItem(this.order, orderItem.id);
        }
    }

    ngOnDestroy() {

    }
    private up() {
        this.pagingInfo.turningPage(-1);
    }

    private down() {
        this.pagingInfo.turningPage(1);
    }

    ngAfterViewInit() {

    }
}